package com.bond.jumber.server.netty;

import com.bond.jumper.common.po.MessageToServer;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import static com.bond.jumper.common.ChannelHolder.LICENSE_ATTRIBUTE_KEY;

/**
 * 处理客户端请求
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<MessageToServer> {

    public NettyServerHandler() {
        super();
    }

    /**
     * 客户端连接
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("NettyServerHandler 客户端连接 ");
        ChannelGroupContext.jumperClientSet.add(ctx.channel());
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageToServer messageToServer) throws Exception {
        //System.out.println("NettyServerHandler 有消息" + messageToServer.getImage().length);
        String license = messageToServer.getLicense();
        if (!ChannelGroupContext.jumperClientChanelMap.containsKey(license)) {
            ChannelGroupContext.jumperClientChanelMap.put(license, ctx.channel());
            ctx.channel().attr(LICENSE_ATTRIBUTE_KEY).set(messageToServer.getLicense());
        }
        if (ChannelGroupContext.webSocketChanelMap.containsKey(license)) {

            Channel websocketChannel = ChannelGroupContext.webSocketChanelMap.get(license);

            String json = "{\"pointX\":\"" + messageToServer.getPointX() + "\",\"pointY\":\"" + messageToServer.getPointY() + "\",\"width\":\"" + messageToServer.getWidth() + "\",\"height\":\"" + messageToServer.getHeight() + "\"}";
            websocketChannel.writeAndFlush(new TextWebSocketFrame(json));
            websocketChannel.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(messageToServer.getImage())));

            //System.out.println(messageToServer.getPointX() + "," + messageToServer.getPointY() + "," + messageToServer.getImage().length);
        }
    }

    /**
     * 发生异常
     *
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }


    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }
}
